package com.liehacker.android.mapservice;

import java.io.IOException;
import java.util.List;

import com.liehacker.android.R;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class C24_GeoCoding extends Activity{
	
	EditText mEdtLat;
	EditText mEdtlot;
	TextView mTvResult;
	Geocoder mGeocoder;
	LocationManager mLocMan;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c24_geocoding);
		
		mGeocoder = new Geocoder(this);
		mTvResult = (TextView)findViewById(R.id.c24_Geo_tvConvertResult);
		
		findViewById(R.id.c24_btngeoconvert).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				List<Address> addr;
				String strLatitude = ((EditText)findViewById(R.id.c24_edtgeolat)).getText().toString();
				String strGitude = ((EditText)findViewById(R.id.c24_edtgeolot)).getText().toString();
				
				try {
				addr = mGeocoder.getFromLocation(Double.parseDouble(strLatitude), Double.parseDouble(strGitude), 5);
				}catch (IOException ex) {
					mTvResult.setText("IO Error : " + ex.getMessage());
					return;
				}
				
				if( addr == null ) {
					mTvResult.setText("No Result!");
					return;
				}
				
				mTvResult.setText("개수 = " + addr.size() + "\n" + addr.get(0) );
			}			
		});
		
		findViewById(R.id.c24_btngeoAddressConvert).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				List<Address> addr;
				String strAddress = ((EditText)findViewById(R.id.c24_edtgeoaddress)).getText().toString();
				
				try {
					addr = mGeocoder.getFromLocationName(strAddress, 5);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					mTvResult.setText("IO Error : " + e.getMessage());
					return;
				}
				
				if(addr == null) {
					mTvResult.setText("NO Result");
					return;
				}
				
				mTvResult.setText("개수 = " + addr.size() + "\n" + addr.get(0));
				
			}			
		});
		
		
	}
}
